#! /usr/bin/env python
# ______________________________________________________________________
"""Module LL1ParserUtil.py

Jonathan Riehl

$Id$
"""
# ______________________________________________________________________
# Module imports

import StringIO
import tokenize

# ______________________________________________________________________
# Function definitions

def mkMyParser (parserKlass):
    """mkMyParser(parserKlass)

    Return a function that parses the LL(1) language defined by the
    given subclass of LL1Parser, parserKlass.

    The nested function myParser(), acts as an adapter from the
    MyFront environment to the LL1Parser parsing interface, thus
    providing a de facto definition of the MyFront parsing protocol."""
    # ____________________________________________________________
    def myParser (text, env):
        """myParser(text, env)

        Specialized function generated by mkMyParser().  Parses
        passed-in text, returning a concrete parse tree and the input
        environment (dictionary)."""
        # XXX - There is going to be some line number mismatch stuff
        # here; look into it and fix it.
        tokenizer_readline = StringIO.StringIO(text).readline
        tokenizer = tokenize.generate_tokens(tokenizer_readline)
        filename = env.get("filename", "<string>")
        parser = parserKlass(tokenizer, filename)
        parser.set_line_offset(env.get("lineno", 1) - 1)
        concrete_tree = parser()
        return concrete_tree, env
    # ____________________________________________________________
    return myParser

# ______________________________________________________________________
# End of LL1ParserUtil.py
